package com.solotiger.douban.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.FrameLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.solotiger.douban.MyApplication;
import com.solotiger.douban.R;
import com.solotiger.douban.biz.model.movie.Subject;
import com.solotiger.douban.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * MovieAdapter
 */
public class MovieAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_THEATER = 1;
    private static final int TYPE_COMING = 2;

    private final LayoutInflater mInflater;
    private final int itemSize;
    private final boolean isTheater;

    private List<Subject> mMovieList;

    public MovieAdapter(Context context, int itemSize, boolean isTheater) {
        this.mInflater = LayoutInflater.from(context);
        this.itemSize = itemSize;
        this.isTheater = isTheater;
        mMovieList = new ArrayList<>();
    }

    public void notifyDataSetChanged(List<Subject> movieList) {
        if (movieList != null) {
            this.mMovieList.addAll(movieList);
        }
        super.notifyDataSetChanged();
    }

    public void clear() {
        this.mMovieList.clear();
        super.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_THEATER: {
                return new TheaterViewHolder(mInflater.inflate(R.layout.item_movie_theater, parent, false),
                        itemSize, null);
            }
            case TYPE_COMING: {
                return new ComingViewHolder(mInflater.inflate(R.layout.item_movie_coming, parent, false),
                        itemSize, null);
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_COMING: {
                ComingViewHolder comingViewHolder = (ComingViewHolder) holder;
                Subject subject = mMovieList.get(position);
                comingViewHolder.setData(subject);
                break;
            }
            case TYPE_THEATER: {
                TheaterViewHolder theaterViewHolder = (TheaterViewHolder) holder;
                Subject subject = mMovieList.get(position);
                theaterViewHolder.setData(subject);
                break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mMovieList == null ? 0 : mMovieList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return isTheater ? TYPE_THEATER : TYPE_COMING;
    }

    private static class TheaterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final OnItemClickListener mItemClickListener;

        private SimpleDraweeView mIvImage;
        private TextView mTitle;
        private TextView mRating;
        private RatingBar mRatingBar;
        private int itemSize;
        private int imageHeight;

        TheaterViewHolder(View itemView, int itemSize, OnItemClickListener itemClickListener) {
            super(itemView);

            itemView.getLayoutParams().height = (int) (itemSize * 1.5);
            this.itemSize = itemSize;

            mIvImage = (SimpleDraweeView) itemView.findViewById(R.id.image);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mRatingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
            mRating = (TextView) itemView.findViewById(R.id.rating);

            imageHeight = (int) (itemSize * 1.5);

//            mIvImage.getLayoutParams().height = imageHeight;
//            mIvImage.getLayoutParams().width = itemSize;

            this.mItemClickListener = itemClickListener;

            itemView.setOnClickListener(this);

        }

        void setData(Subject subject) {
            Uri uri = Uri.parse(subject.getImages().getMedium());
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setLocalThumbnailPreviewsEnabled(true)
                    .setResizeOptions(new ResizeOptions(itemSize, imageHeight))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(mIvImage.getController())
                    .setImageRequest(request)
                    .build();
            mIvImage.setController(controller);

            mTitle.setText(subject.getTitle());
            mRatingBar.setRating((float) subject.getRating().getAverage() / 2);
            mRating.setText(String.valueOf(subject.getRating().getAverage()));
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null && v == itemView) {
                mItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    private static class ComingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final OnItemClickListener mItemClickListener;

        private SimpleDraweeView mIvImage;
        private TextView mTitle;
        private TextView mCollect;
        private TextView mDate;
        private int itemSize;
        private int imageHeight;

        ComingViewHolder(View itemView, int itemSize, OnItemClickListener itemClickListener) {
            super(itemView);

            itemView.getLayoutParams().height = (int) (itemSize * 1.8);
            this.itemSize = itemSize;

            mIvImage = (SimpleDraweeView) itemView.findViewById(R.id.image);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mCollect = (TextView) itemView.findViewById(R.id.collect);
            mDate = (TextView) itemView.findViewById(R.id.date);

            imageHeight = (int) (itemSize * 1.5);

//            mIvImage.getLayoutParams().height = imageHeight;
//            mIvImage.getLayoutParams().width = itemSize;

            this.mItemClickListener = itemClickListener;

            itemView.setOnClickListener(this);

        }

        void setData(Subject subject) {
            Uri uri = Uri.parse(subject.getImages().getMedium());
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setLocalThumbnailPreviewsEnabled(true)
                    .setResizeOptions(new ResizeOptions(itemSize, imageHeight))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(mIvImage.getController())
                    .setImageRequest(request)
                    .build();
            mIvImage.setController(controller);

            mTitle.setText(subject.getTitle());

            mCollect.setText(MyApplication.getInstance().getString(R.string.txt_collect_count, String.valueOf(subject.getCollect_count())));

            mDate.setText(subject.getMainland_pubdate());
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null && v == itemView) {
                mItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
